using LitJson;
using UnityEngine;

namespace BToolkit
{
    /// <summary>
    /// 重要说明：MainActivity.jar必须放在Assets根目录下的Plugins内，子目录的Plugins无效
    /// </summary>
    public class MainActivity : MonoBehaviour
    {
        public delegate void ActivityResultAction(int requestCode, int resultCode);
        public delegate void PermissionResultAction(int requestCode, string permission, bool isGeted);
        private static MainActivity _instance;
        public static MainActivity Instance
        {
            get
            {
                if (!_instance)
                {
                    _instance = GameObject.FindObjectOfType<MainActivity>();
                    if (!_instance)
                    {
                        GameObject go = new GameObject("[MainActivity]");
                        DontDestroyOnLoad(go);
                        _instance = go.AddComponent<MainActivity>();
                    }
                }
                return _instance;
            }
        }
        private AndroidJavaObject mainActivity;
        private ActivityResultAction onActivityResult;
        private PermissionResultAction onPermissionResult;

        private MainActivity()
        {
            if (Application.platform == RuntimePlatform.Android)
            {
                AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                mainActivity = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
            }
        }

        public void RegisterActivityResult(ActivityResultAction onActivityResult)
        {
            this.onActivityResult = onActivityResult;
            if (Application.platform == RuntimePlatform.Android)
            {
                mainActivity.Call("registerActivityResult", gameObject.name, "OnActivityResultCallback");
            }
        }

        private void OnActivityResultCallback(string json)
        {
            JsonData data = JsonMapper.ToObject(json);
            int requestCode = (int)data["requestCode"];
            int resultCode = (int)data["resultCode"];
            onActivityResult?.Invoke(requestCode, resultCode);
        }

        public void RegisterPermissionResult(PermissionResultAction onPermissionResult)
        {
            this.onPermissionResult = onPermissionResult;
            if (Application.platform == RuntimePlatform.Android)
            {
                mainActivity.Call("registerPermissionResult", gameObject.name, "OnPermissionResultCallback");
            }
        }

        private void OnPermissionResultCallback(string json)
        {
            JsonData data = JsonMapper.ToObject(json);
            int requestCode = (int)data["requestCode"];
            string permission = (string)data["permission"];
            bool grantResult = (bool)data["grantResult"];
            onPermissionResult?.Invoke(requestCode, permission, grantResult);
        }

    }
}